package com.ymm.system.core.facade;

import com.ymm.system.api.constants.LltorderEntity;
import com.ymm.system.api.facade.SystemPriceService;
import com.ymm.system.api.utils.Result;
import com.ymm.system.api.utils.ResultCodeEnum;
import com.ymm.system.core.config.websocket.WebSocket;
import com.ymm.system.core.repo.DriverRelatedRepo;
import com.ymm.system.core.repo.ShipperRelatedRepo;
import com.ymm.system.dao.mapper.LltorderMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * @auther ZhangHongyuan
 * @date 2022/8/9 19:08
 */

@Service(version = "server")
@Component
public class SystemPriceServiceImpl implements SystemPriceService {

    @Resource
    private WebSocket webSocket;

    @Autowired
    private DriverRelatedRepo driverRelatedRepo;

    @Autowired
    private LltorderMapper lltorderMapper;

    @Autowired
    private ShipperRelatedRepo shipperRelatedRepo;

    /**
     * 抽佣比例
     */
    private static final double EXTRACT_COMMISSION_PERCENTAGE = 0.1;

    @Override
    public Result orderSettle(Long orderId) {
        if (orderId == null) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        LltorderEntity lltorderEntity = lltorderMapper.getOrderDetailsByOrderId(orderId);
        Integer orderPrice = lltorderEntity.getOrderPrice();
        Long driverId = lltorderEntity.getDriverId();
//        Long shipperId = lltorderEntity.getShipperId();
        driverRelatedRepo.addDriverProfit(driverId,
        BigDecimal.valueOf((1 - EXTRACT_COMMISSION_PERCENTAGE) * orderPrice));//结算司机货款
        webSocket.sendMessage("你的"+BigDecimal.valueOf((1 - EXTRACT_COMMISSION_PERCENTAGE) * orderPrice).doubleValue()+"块钱已经给你！");
//        //先获取会员积分，更新会员等级之后再进行返现
//        Integer shipperVipPoint = shipperRelatedRepo.getShipperVipPoint(shipperId);
//        //更新货主会员积分
//        shipperVipPoint += orderPrice / 100;
//        shipperRelatedRepo.updateShipperVipPoint(shipperId,shipperVipPoint);
//        Integer finalVipPoint=shipperRelatedRepo.getShipperVipPoint(shipperId);
//        //查找货主会员等级
//        Integer shipperVipLevel = shipperRelatedRepo.getShipperVipLevel(shipperId);
//        //更新货主会员等级
//        Integer shipperNewLevel = shipperVIPLevelRefresh(shipperVipPoint);
//        shipperRelatedRepo.updateShipperVipLevel(shipperId,shipperNewLevel);
//        //最后进行返现
//        if (!shipperVipLevel.equals(0)) {
//            //TODO 货主返现
//            shipperCashBack(shipperVipLevel, orderPrice);
//
//        }
        return Result.success();
    }

    @Override
    public Result orderRefund(Long orderId) {
        if (orderId == null) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        LltorderEntity lltorderEntity = lltorderMapper.getOrderDetailsByOrderId(orderId);
        Integer orderPrice = lltorderEntity.getOrderPrice();
        Long shipperId = lltorderEntity.getShipperId();
        //TODO 对应货主货款返回
        return Result.success();
    }

}
